<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app">
    <div class="demo-input-size">
        <el-input
                size="medium"
                placeholder="请输入科室名"
                suffix-icon="el-icon-date"
                style="display: inline-block;width: 200px;"
                v-model="searchInfo.kname"
        >
        </el-input>
        <el-button icon="el-icon-search" type="success"  @click="searchKes()">搜索科室</el-button>
        <el-button icon="el-icon-circle-plus-outline" type="warning"  @click="showAddDialog()">添加科室</el-button>
        <el-button icon="el-icon-sort" type="warning"  @click="exportKes()">导出</el-button>
        <a style="text-decoration: none" href="/ssm/kestemp.xlsx">下载模板</a>
        <form id="upform">
            <input name="haha" type="file">
        </form>
        <el-button icon="el-icon-sort"  type="warning" @click="uploadKes()" >导入科室信息</el-button>

        <el-table
                :data="kesArr"
                border
                style="width: 100%">
            <el-table-column
                    prop="id"
                    label="编号"
                    width="80">
            </el-table-column>
            <el-table-column
                    prop="kname"
                    label="科室名称"
            >
            </el-table-column>
            <el-table-column
                    prop="state"
                    label="科室状态">

            </el-table-column>
            <el-table-column
                    prop="oid"
                    label="关联id">

            </el-table-column>
            <el-table-column
                    fixed="right"
                    label="操作"
            >
                <template slot-scope="scope">
                    <el-button icon="el-icon-delete-solid" type="warning" circle
                               @click="deleteKes(scope.row.id)"></el-button>
                    <el-button icon="el-icon-s-tools" type="primary" circle @click="showKes(scope.row.id)"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                background
                @current-change="handleCurrentChange"
                layout="prev, pager, next"
                :total="totalCount">
        </el-pagination>
    </div>
    <el-dialog title="添加新科室" :visible.sync="dialogFormVisible">
        <el-form :model="kesInfo" :rules="rules" ref="kesInfo">
            <el-form-item label="科室名称" prop="kname">
                <el-input v-model="kesInfo.kname"></el-input>
            </el-form-item>
            <el-form-item label="科室状态" prop="state">
                <el-select v-model="kesInfo.state" placeholder="请选择科室状态">
                    <el-option label="开启" value="开启"></el-option>
                    <el-option label="关闭" value="关闭"></el-option>
                    <el-option label="冻结" value="冻结"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="关联id" prop="oid">
                <el-input v-model="kesInfo.oid"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addKes()">添 加</el-button>
        </div>
    </el-dialog>
    <el-dialog title="修改科室" :visible.sync="updatedialogFormVisible">
        <el-form :model="kesInfo" :rules="rules" ref="kesInfo">
            <el-form-item label="科室名称" prop="kname">
                <el-input v-model="kesInfo.kname"></el-input>
            </el-form-item>
            <el-form-item label="科室状态" prop="state">
                <el-select v-model="kesInfo.state" placeholder="请选择科室状态">
                    <el-option label="开启" value="开启"></el-option>
                    <el-option label="关闭" value="关闭"></el-option>
                    <el-option label="冻结" value="冻结"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="关联id" prop="oid">
                <el-input v-model="kesInfo.oid"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="updatedialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="updateKes()">修 改</el-button>
        </div>
    </el-dialog>
</div>
<script>
    var danghui = new Vue({
        el: "#app",
        data: {
            searchInfo: {},
            kesArr: [],
            totalCount: 0,
            dialogFormVisible: false,
            updatedialogFormVisible: false,
            kesInfo: {},
            rules: {
                kname: [
                    {required: true, message: '请输入科室名称', trigger: 'blur'},
                ],
                state: [
                    {required: true, message: '请输入此科室的状态', trigger: 'blur'},
                ],
                oid: [
                    {required: true, message: '请输入此科室的关联id', trigger: 'blur'},
                ]
            }
        },
        methods: {

            uploadKes(){
                getData(1,10);
                //使用js写好的formdata类  读取表单中的信息
                var kesData = new FormData(upform);
                //通过Ajax将data发送给后台  $.post
                $.ajax({
                    url:"/ssm/kessource/upload",
                    type:"POST",
                    data:kesData,
                    processData:false,
                    contentType:false,
                    success:function(backData){
                    }
                });
            },

            exportKes() {
                window.location.href = "/ssm/kessource/download?name=" + danghui.searchInfo.kname + "&pageNo=1&pageCount=10";
            },

            updateKes() {
                danghui.$refs["kesInfo"].validate((valid) => {
                    if (valid) {
                        $.post("/ssm/kes/update", danghui.kesInfo, function (backData) {
                            if (backData.code == 1) {
                                danghui.updatedialogFormVisible = false;
                                danghui.$refs["kesInfo"].resetFields();
                                danghui.$message.success("修改成功");
                                getData(1, 10);
                            } else {
                                danghui.$message.error("修改失败");
                            }
                        });
                    }
                });
            },
            showKes(id) {
                $.get("/ssm/kes/getKes/" + id, function (backData) {
                    danghui.kesInfo = backData.data;
                });
                danghui.updatedialogFormVisible = true;
            },

            addKes() {
                danghui.$refs["kesInfo"].validate((valid) => {
                    if (valid) {
                        $.post("/ssm/kes/add", danghui.kesInfo, function (backData) {
                            if (backData.code == 1) {
                                danghui.dialogFormVisible = false;
                                danghui.$refs["kesInfo"].resetFields();
                                danghui.$message.success("添加成功");
                                getData(1, 10);
                            } else {
                                danghui.$message.error("添加失败");
                            }
                        });
                    }
                });
            },
            showAddDialog() {
                danghui.dialogFormVisible = true;
            },

            deleteKes(id) {
                danghui.$confirm('此操作将永久删除该科室, 是否继续?', '温馨提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $.get("/ssm/kes/delete/" + id, function (backData) {
                        danghui.$message.success(backData.msg);
                        getData(1, 10)
                    });
                });
            },

            searchKes() {
                getData(1, 10);
            },
            handleCurrentChange(val) {
                getData(val, 10)
            }
        }
    });

    function getData(a, b) {
        danghui.searchInfo.pageNo = a;
        danghui.searchInfo.pageCount = b;
        $.post("/ssm/kes/query", danghui.searchInfo, function (backData) {
            danghui.kesArr = backData.data.currentData;
            danghui.totalCount = backData.data.totalCount;
        });
    }

    getData(1, 10);

</script>
</body>
</html>